
package com.loong.loong.hik.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.api.dto.HikPeopleIdentifyDto;
import com.loong.loong.hik.entity.PersonPlan;
import com.loong.loong.hik.mapper.PersonPlanMapper;
import com.loong.loong.hik.property.request.face.FaceRelationRequestDto;
import com.loong.loong.hik.property.result.face.FaceGroupResultVo;
import com.loong.loong.hik.property.result.face.HikPeopleResultVo;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.HikFaceVService;
import com.loong.loong.hik.service.PersonPlanService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 人员识别计划
 *
 * @author jinyiang
 * @date 2023-03-29 14:38:30
 */
@Service
public class PersonPlanServiceImpl extends ServiceImpl<PersonPlanMapper, PersonPlan> implements PersonPlanService {

	@Resource
	private HikFaceVService hikFaceVService;

	@Override
	public IPage<PersonPlan> findPage(Page page, PersonPlan personPlan) {
		return baseMapper.queryPage(page,personPlan);
	}

	@Override
	public R add(PersonPlan personPlan) {
		//调接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			HikPeopleIdentifyDto hikPeopleIdentifyDto = new HikPeopleIdentifyDto();
			hikPeopleIdentifyDto.setName(personPlan.getName());
			List<String> cameras = new ArrayList<>();
			cameras.add(personPlan.getCameraIndexCodes());
			hikPeopleIdentifyDto.setCameraIndexCodes(cameras);

			hikPeopleIdentifyDto.setDescription(personPlan.getDescription());
			List<String> faceGroups = new ArrayList<>();
			faceGroups.add(personPlan.getFaceGroupIndexCodes());
			hikPeopleIdentifyDto.setFaceGroupIndexCodes(faceGroups);
			List<String> recognitionResourceIndexCodes = new ArrayList<>();
			recognitionResourceIndexCodes.add(personPlan.getRecognitionResourceIndexCodes());
			hikPeopleIdentifyDto.setRecognitionResourceIndexCodes(recognitionResourceIndexCodes);
			hikPeopleIdentifyDto.setThreshold(personPlan.getThreshold());
			HikPeopleIdentifyDto.TimeBlock timeBlock = new HikPeopleIdentifyDto.TimeBlock();
			timeBlock.setDayOfWeek(personPlan.getDayOfWeek());
			HikPeopleIdentifyDto.TimeBlock.TimeRange timeRange = new HikPeopleIdentifyDto.TimeBlock.TimeRange();
			timeRange.setStartTime(DateUtil.format(personPlan.getSnapStartTime(),"HH:mm"));
			timeRange.setEndTime(DateUtil.format(personPlan.getSnapEndTime(),"HH:mm"));
			List<HikPeopleIdentifyDto.TimeBlock.TimeRange> timeRanges = new ArrayList<>();
			timeRanges.add(timeRange);
			timeBlock.setTimeRangeList(timeRanges);
			List<HikPeopleIdentifyDto.TimeBlock> timeBlocks = new ArrayList<>();
			timeBlocks.add(timeBlock);
			hikPeopleIdentifyDto.setTimeBlockList(timeBlocks);
			try {
				HikPeopleResultVo hikPeopleResultVo = null;
				if ("1".equals(personPlan.getFlag())){

					hikPeopleResultVo = hikFaceVService.addBlack(hikPeopleIdentifyDto);
				}else{
					hikPeopleResultVo = hikFaceVService.addWhite(hikPeopleIdentifyDto);
				}
				if (hikPeopleResultVo!=null&&"0".equals(hikPeopleResultVo.getCode())){
					String data = hikPeopleResultVo.getData();
					personPlan.setIndexCode(data);
				}else{
					return R.failed(hikPeopleResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return add(personPlan);
	}

	/**
	 *
	 * @param personPlan
	 * @return
	 */
	@Override
	public R update(PersonPlan personPlan) {
		//调接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			//updateBlack
			HikPeopleIdentifyDto hikPeopleIdentifyDto = new HikPeopleIdentifyDto();
			hikPeopleIdentifyDto.setName(personPlan.getName());
			List<String> cameras = new ArrayList<>();
			cameras.add(personPlan.getCameraIndexCodes());
			hikPeopleIdentifyDto.setCameraIndexCodes(cameras);

			hikPeopleIdentifyDto.setDescription(personPlan.getDescription());
			List<String> faceGroups = new ArrayList<>();
			faceGroups.add(personPlan.getFaceGroupIndexCodes());
			hikPeopleIdentifyDto.setFaceGroupIndexCodes(faceGroups);
			List<String> recognitionResourceIndexCodes = new ArrayList<>();
			recognitionResourceIndexCodes.add(personPlan.getRecognitionResourceIndexCodes());
			hikPeopleIdentifyDto.setRecognitionResourceIndexCodes(recognitionResourceIndexCodes);
			hikPeopleIdentifyDto.setThreshold(personPlan.getThreshold());
			HikPeopleIdentifyDto.TimeBlock timeBlock = new HikPeopleIdentifyDto.TimeBlock();
			timeBlock.setDayOfWeek(personPlan.getDayOfWeek());
			HikPeopleIdentifyDto.TimeBlock.TimeRange timeRange = new HikPeopleIdentifyDto.TimeBlock.TimeRange();
			timeRange.setStartTime(DateUtil.format(personPlan.getSnapStartTime(),"HH:mm"));
			timeRange.setEndTime(DateUtil.format(personPlan.getSnapEndTime(),"HH:mm"));
			List<HikPeopleIdentifyDto.TimeBlock.TimeRange> timeRanges = new ArrayList<>();
			timeRanges.add(timeRange);
			timeBlock.setTimeRangeList(timeRanges);
			List<HikPeopleIdentifyDto.TimeBlock> timeBlocks = new ArrayList<>();
			timeBlocks.add(timeBlock);
			hikPeopleIdentifyDto.setTimeBlockList(timeBlocks);
			try {
				FaceGroupResultVo faceGroupResultVo = null;
				if ("1".equals(personPlan.getFlag())){

					faceGroupResultVo = hikFaceVService.updateBlack(hikPeopleIdentifyDto);
				}else{
					faceGroupResultVo = hikFaceVService.updateWhite(hikPeopleIdentifyDto);
				}
				if (faceGroupResultVo!=null&&"0".equals(faceGroupResultVo.getCode())){

				}else{
					return R.failed(faceGroupResultVo.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return update(personPlan);
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@Override
	public R remove(Integer id) {

		PersonPlan personPlan = getById(id);
		if (personPlan==null){
			return R.failed("该数据已删除或者不存在");
		}
		if (HikFaceUrl.SWITCH.equals("Y")){
			//delBlack
			FaceRelationRequestDto faceRelationRequestDto = new FaceRelationRequestDto();
			List<String> lists = new ArrayList<>();
			lists.add(personPlan.getIndexCode());
			try {
				FaceGroupResultVo faceGroupResultVo =null;
				if ("1".equals(personPlan.getFlag())){
					faceGroupResultVo = hikFaceVService.delBlack(faceRelationRequestDto);
				}else{
					faceGroupResultVo = hikFaceVService.delWhite(faceRelationRequestDto);
				}
				if (faceGroupResultVo!=null){
					if ("0".equals(faceGroupResultVo.getCode())){

					}else{
						return R.failed(faceGroupResultVo.getMsg());
					}
				}else{
					return R.failed("接口调用异常");
				}
			} catch (Exception e) {
				e.printStackTrace();
				return R.failed(e.getMessage());
			}
		}
		return R.ok();
	}
}
